(function () {
  new Vue({
    el: '#contractCustom',
    data() {
      return {
        data: [],//字段列表（自定义）
        data1:[],//字段列表（内置）
        LXdata: [], //类型
        isWhether: [{ name: '是', code: 'true' }, { name: '否', code: 'false' }],
        isAddFlag: false, //新增按钮
        addItem: [],//新增(单个item)
        templateList:{
          customizeColumns:[],
          systemColumns:[],
        },//模板类型
        texthover:false,
        textLong:'',
        tophover:'',
        lefthover:'',
      }
    },
    methods: {
      //获取自定义字段
      getData() {
        var that = this
        hlhttp.get({
          url: 'configure/customize_column',
          data: {
            "module": "ZHSH",// 所属模块code
            "config_name": "ZS_CUSTOMIZE_COLUMNS"// 配置名称
          },
          success: function (res) {
            let arr = res.customizeColumns
            arr.map(item => {
              item.dict_name = item.dictName,
                item.dict_name_copy = item.dictName,
                item.isInput_name = false,
                item.field_type = [item.fieldType],
                item.field_range = item.fieldRange,
                item.field_range_copy = item.fieldRange,
                item.isInput_range = false,
                item.is_required = [item.isRequired],
                item.template_flag = [item.templateFlag],
                item.template_types = item.templateTypes,
                item.template_key = item.templateKey
            })
            that.data = arr
            that.data1 = res.systemColumns
          }
        })
      },
      //获取类型字典
      getLXData() {
        var that = this
        hlhttp.get({
          url: 'configure/customize_column',
          data: {
            "module": "CMMN",// 所属模块code
            "config_name": "CM_CUSTOM_COLUMN_TYPE"// 配置名称
          },
          success: function (res) {
            let arr = [...res.customizeColumns,...res.systemColumns]
            arr.map(item => {
              item.name = item.displayName
              item.code = item.configCode
            })
            that.LXdata = arr
          }
        })
      },
      // 获取打印模板类型
      getTemplateType() {
        var that = this
        hlhttp.get({
          url: 'configure/customize_column',
          data: {
            "module": "CMMN",// 所属模块code
            "config_name": "CM_PRINT_TEMPLATE_TYPE"// 配置名称
          },
          success: function (res) {
            res.systemColumns.map(item =>{
              item.name = item.dictName
              item.code = item.configCode
            })
            that.templateList = res
          }
        })
      },
      //添加一行
      add() {
        let flag = false
        this.addItem.some(item => {
          if (!item.dict_name || item.isInput_name || !item.field_type.length || !item.field_range || !item.template_types.length) {
            hl.toast('请完成或确定添加字段')
            flag = true
          }
        })
        if(flag){
          return
        }
        this.isAddFlag = true
        this.addItem = []
        this.addItem.push({
          dict_name: '',
          dict_name_copy: '',
          isInput_name: true,
          field_type: [],
          field_range: '',
          field_range_copy: '',
          isInput_range: true,
          is_required: ['true'],
          template_flag: ['true'],
          template_types: [],
          template_key: '#'
        })
      },
      // 检测一行是否填写完整并请求接口进行更改
      checkItem(item, type) {
        console.log(item)
        if (!item.dict_name_copy) {
          hl.toast('请输入自定义名称')
          return
        }
        if (!item.field_type.length) {
          hl.toast('请选择类型')
          return
        }
        if (!item.field_range_copy && item.field_type + '' != 'date') {
          hl.toast('请输入取值范围')
          return
        }
        if(item.field_type+'' == 'date'){
          item.field_range_copy = ''
        }
        if(!item.template_types.length && item.template_flag +'' == 'true' ){
          hl.toast('请选择模板类型')
          return
        }
        if(item.template_flag +'' == 'false'){
          item.template_types = []
        }
        item.dict_name = item.dict_name_copy,
        item.isInput_name = false,
        item.field_range = item.field_range_copy || '#',
        item.isInput_range = false
        if (type == 'edit') {
          this.editItem(item)
        } else {
          this.sureAdd();
        }

      },
      // 确认添加字段
      sureAdd() {
        let data = {
          "dict_name": this.addItem[0].dict_name,// 自定义字段名称
          "field_type": this.addItem[0].field_type + '',// 字段类型
          "field_range": this.addItem[0].field_range + '',// 取值范围
          "is_required": this.addItem[0].is_required + '',// 是否必填
          "template_flag": this.addItem[0].template_flag + '',// 是否关联模板 true,false，默认传false
          "template_types": this.addItem[0].template_types,// 模板类型，默认传空集合[]
          "template_key": "#"// 主键id，新增时传#
        }
        var that = this
        hlhttp.post({
          url: 'configure/customize_column',
          data: {
            "module": "ZHSH",// 所属模块code
            "config_name": "ZS_CUSTOMIZE_COLUMNS",// 配置名称
            "customize_field": JSON.stringify(data)
          },
          success: function (res) {
            hl.toast('添加成功')
            that.getData()
            that.addItem = []
          }
        })
      },
      // 停用、启用
      disableItem(item) {
        var that = this
        hlhttp.put({
          url: 'configure/customize_column/_valid',
          data: {
            "module": "ZHSH",// 所属模块code
            "config_name": "ZS_CUSTOMIZE_COLUMNS",// 配置名称
            "dict_code": item.configCode,
            "opt": item.isValid == 'true' ? 'N' : 'Y'
          },
          success: function (res) {
            hl.toast('更改成功')
            that.getData()
          }
        })
      },
      //删除
      delItem(code) {
        var that = this
        hlhttp.delete({
          url: 'configure/customize_column?module=' + 'ZHSH' + '&config_name=' + 'ZS_CUSTOMIZE_COLUMNS' + '&dict_code_list=' + code,
          data: {
            "module": "ZHSH",// 所属模块code
            "config_name": "ZS_CUSTOMIZE_COLUMNS",// 配置名称
            "dict_code_list": code,
          },
          success: function (res) {
            hl.toast('删除成功')
            that.getData()
          }
        })
      },
      //编辑
      editItem(item) {
        let that = this
        let data = {
          "dict_name": item.dict_name,
          "field_type": item.field_type + '',
          "field_range": item.field_range + '',
          "is_required": item.is_required + '',// 是否必填
          "template_flag": item.template_flag + '',// 是否关联模板 true,false，默认传false
          "template_types": item.template_types,// 模板类型，默认传空集合[]
          "template_key": item.template_key + ''// 主键id
        }
        hlhttp.put({
          url: 'configure/customize_column',
          data: {
            "module": "ZHSH",// 所属模块code
            "config_name": "ZS_CUSTOMIZE_COLUMNS",// 配置名称
            "dict_code": item.configCode,
            "customize_field": JSON.stringify(data)
          },
          success: function (res) {
            hl.toast('编辑成功')
            that.getData()
          }
        })
      },
      //鼠标移除移除取值范围(N内置，Z自定义)
      showHover(val,index,type,e){
        if(val.length < 10){
          return
        }
        let t = ''
        let l = ''
        if(type == 'N'){
          t = this.$refs.offsetTopBox1.offsetTop + 45 + 25 + index*46
          l = e.srcElement.parentNode.offsetLeft
        }
        if(type == 'Z'){
          t = this.$refs.offsetTopBox2.offsetTop + 45 + 25 + index*46
          l = e.srcElement.parentNode.offsetLeft - 25
        }
        this.lefthover = l + 'px';
        this.tophover = t + 'px';
        this.texthover = true;
        this.textLong = val;
      },
      // 转化文字类型（内置）
      typeText(val){
        let text = ''
        this.LXdata.map(item =>{
          if(item.configCode == val){
            text =  item.dictName
          }
        })
        return text
      },
      // 模板类型（内置）
      templateTyle(val){
        let text = ''
        if(val.includes('PCRT')){
          text = text + '合同模板'
        }
        if(val.includes('PBLL')){
          text = text + '账单模板'
        }
        return text
      }
    },
    mounted() {
      this.getData()
      this.getLXData()
      this.getTemplateType()
    },
  })
}())
